package com.ffapp.f9164.td.util;

import android.content.Context;
import android.widget.Toast;

import com.clj.fastble.utils.HexUtil;
import com.ffapp.f9164.td.R;
import com.ffapp.f9164.td.basis.BasisApp;
import com.ffapp.f9164.td.bean.ParamListData;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cz.msebera.android.httpclient.util.TextUtils;
import my.ActivityTool;

public class PassWayUtil {


    /**
     * 通道属性
     */
    static String sDeviceInface; //硬件接口
    static String sChannelOnOff;// 数据采样开关
    static String sFactorName;//要素标识  通道标识
    static String sSampBaseValue;//采样基值
    static String sUnit;//单位          采集数据单位
    static String sPrecision;//精度     采集数据精度
    static String sUnitSize;//采集数据大小
    static String sPowerOnDelay;//上电延时
    static String sStorageTimeInterval;//存储时间间隔
    static String sSampTimeInterval;//采样间隔
    static String fireConfSysType;  //消防系统类型
    static String fireConfPartType;  //消防部件类型
    static String fireConfAnType;  //消防模拟量类型
    static String sUpReportTime;//上报间隔

    /**
     * /通信参数
     */
    //RS系列
    static String sConnMode;//总线通信模式
    static String sSerial_Baudrate;   //波特率
    static String sSerial_Com;     //数据位sSerial_Com.substring(0, 1)     停止位sSerial_Com.substring(2, 3)    校验位sSerial_Com.substring(1, 2)
    //总线通信模式MODBUS
    static String sMODBUS_deviceAdd;          //设备地址
    static String sMODBUS_functionCode;       //功能码
    static String sMODBUS_registerAdd;           //寄存器地址
    static String sMODBUS_registerNumber;         //寄存器个数
    static String sMODBUS_universalSerialPort;         //通用串口

    //ADC系列
    static String sElectricity_sensor;//电流传感器       ADC类型
    static String sMaxRange;//上量程
    static String sMinRange;//下量程
    static String sMaxValue;//最大值  输入电流
    static String sMinValue;//最小值  输入电流


    /**
     * 报警参数
     */
    static String upperLimitValue;//上限值
    static String lowerLimitValue;//下限值
    static String threshold;//报警阈值
    static String diAlarm;      //报警触发方式
    static String sAlarmContext;//报警内容

    /**
     * 扩充
     */
    static String dataBits;     // 数据位
    static String stopBits;     // 停止位
    static String checkBits;     // 校验位


    /**
     * @param sMsgObjString  at+show的内容
     * @param context
     * @param msgIndexOfMsg  通道属性的关键字
     * @param msgIndexOfCom  通信参数的关键字
     * @param msgIndexOfFire 消防系统类别  消防部件类别  消防模拟量类别的关键词
     * @param dataList       数据都在添加在这个list里面
     */
    public static void initPassWayData(String sMsgObjString, Context context, String msgIndexOfMsg, String msgIndexOfCom, String msgIndexOfFire, List<ParamListData> dataList, boolean isChange) {

        sDeviceInface = ""; //硬件接口
        sChannelOnOff = "";// 数据采样开关
        sFactorName = "";//要素标识  通道标识
        sSampBaseValue = "";//采样基值
        sUnit = "";//单位          采集数据单位
        sPrecision = "";//精度     采集数据精度
        sUnitSize = "";//采集数据大小
        sPowerOnDelay = "";//上电延时
        sStorageTimeInterval = "";//存储时间间隔
        sSampTimeInterval = "";//采样间隔
        fireConfSysType = "";  //消防系统类型
        fireConfPartType = "";  //消防部件类型
        fireConfAnType = "";  //消防模拟量类型
        sUpReportTime = "";//上报间隔
        sConnMode = "";//总线通信模式
        sSerial_Baudrate = "";   //波特率
        sSerial_Com = "";     //数据位sSerial_Com.substring(0, 1)     停止位sSerial_Com.substring(2, 3)    校验位sSerial_Com.substring(1, 2)
        sMODBUS_deviceAdd = "";          //设备地址
        sMODBUS_functionCode = "";       //功能码
        sMODBUS_registerAdd = "";           //寄存器地址
        sMODBUS_registerNumber = "";         //寄存器个数
        sMODBUS_universalSerialPort = "";         //通用串口
        sElectricity_sensor = "";//电流传感器       ADC类型
        sMaxRange = "";//上量程
        sMinRange = "";//下量程
        sMaxValue = "";//最大值  输入电流
        sMinValue = "";//最小值  输入电流
        upperLimitValue = "";//上限值
        lowerLimitValue = "";//下限值
        threshold = "";//报警阈值
        diAlarm = "";     //报警触发方式
        sAlarmContext = "";//报警内容

        dataBits = "";     // 数据位
        stopBits = "";     // 停止位
        checkBits = "";     // 校验位

        int iIndexHW_Chn1 = sMsgObjString.indexOf(msgIndexOfMsg);
        if (iIndexHW_Chn1 != -1) {
            String sHW_Chn1 = SubstringUtil.subString(sMsgObjString, msgIndexOfMsg);
            if (sHW_Chn1 != null && sHW_Chn1.trim().length() > 0) {
                String[] splitHW_Chn1 = sHW_Chn1.split(",");
                if (splitHW_Chn1.length >= 14) {
                    sDeviceInface = splitHW_Chn1[0]; //硬件接口
                    sChannelOnOff = splitHW_Chn1[1];// 通道开关  数据采样开关
                    sPowerOnDelay = splitHW_Chn1[2];//上电延时
                    sFactorName = splitHW_Chn1[3];//要素标识  通道标识
                    sUnit = splitHW_Chn1[4];//单位   采集数据单位
                    sUnitSize = splitHW_Chn1[5];//元素单元大小  采集数据大小
                    sStorageTimeInterval = splitHW_Chn1[6];//存储时间间隔
                    sPrecision = splitHW_Chn1[7];//数据采集精度
                    sSampTimeInterval = splitHW_Chn1[8];//数据采样间隔
                    sUpReportTime = splitHW_Chn1[9];//上报间隔
                    upperLimitValue = splitHW_Chn1[10];//上限值
                    lowerLimitValue = splitHW_Chn1[11];//下限值
                    threshold = splitHW_Chn1[12];//报警阈值
                    sSampBaseValue = splitHW_Chn1[13];//采样基值
                    if (splitHW_Chn1.length == 15) {
                        sAlarmContext = splitHW_Chn1[14];//报警内容
                    }
                }
            }
        }


        dataList.add(new ParamListData("硬件接口", 2, "" + sDeviceInface, "", "", false, isChange));
        List<String> openOrClose = Arrays.asList(context.getResources().getStringArray(R.array.openOrClose));
        dataList.add(new ParamListData("数据采样开关", 1, "" + getKeyFromList(sChannelOnOff, openOrClose), openOrClose, "", "", false, isChange));
        dataList.add(new ParamListData("通道标识", 2, "" + sFactorName, "", "", true, isChange));
        dataList.add(new ParamListData("采样基值", 2, "" + sSampBaseValue, "", "", true, isChange));
        dataList.add(new ParamListData("采集数据单位", 2, "" + sUnit, "", "", true, isChange));
        List<String> sPrecisionList = Arrays.asList(context.getResources().getStringArray(R.array.precision));
        dataList.add(new ParamListData("采集数据精度", 1, "" + sPrecision, sPrecisionList, "", "", true, isChange));
        List<String> unitSizeList = Arrays.asList(context.getResources().getStringArray(R.array.unitSize));
        dataList.add(new ParamListData("采集数据大小", 1, "" + sUnitSize, unitSizeList, "", "(byte)", true, isChange));
        dataList.add(new ParamListData("上电延迟", 2, "" + sPowerOnDelay, "", "(0~255s)", true, isChange));
        dataList.add(new ParamListData("数据存储间隔", 2, "" + sStorageTimeInterval, "", "(min)", true, isChange));
        dataList.add(new ParamListData("数据采样间隔", 2, "" + sSampTimeInterval, "", "(5~65535s)", true, isChange));
        dataList.add(new ParamListData("上限值", 2, "" + upperLimitValue, "AT+CHALM", "", true, isChange));
        dataList.add(new ParamListData("下限值", 2, "" + lowerLimitValue, "AT+CHALM", "", true, isChange));
        List<String> diAlarmList = Arrays.asList(context.getResources().getStringArray(R.array.diAlarm));
        dataList.add(new ParamListData("报警触发方式", 1, "" + getDiAlarm(upperLimitValue, lowerLimitValue), diAlarmList, ""));
        dataList.add(new ParamListData("报警阈值", 2, "" + threshold, "AT+CHALM", "", true, isChange));
        dataList.add(new ParamListData("报警内容", 2, "" + sAlarmContext, "AT+CHALMTXT", "", true, isChange));

        int iIndexCH1_Com_Cfg = sMsgObjString.indexOf(msgIndexOfCom);
        if (iIndexCH1_Com_Cfg != -1) {
            String sCH1_Com_Cfg = SubstringUtil.subString(sMsgObjString, msgIndexOfCom);
            if (sCH1_Com_Cfg.startsWith("RS")) {
                sConnMode = "";
                String[] splitCH1_Com_Cfg = sCH1_Com_Cfg.split(",");
                if (splitCH1_Com_Cfg.length >= 4) {
                    sSerial_Baudrate = splitCH1_Com_Cfg[1]; //波特率
                    sSerial_Com = splitCH1_Com_Cfg[2]; //数据位 停止位校验位
                    sConnMode = splitCH1_Com_Cfg[3];//通信模式
                    sMODBUS_deviceAdd = splitCH1_Com_Cfg[4];  //设备地址
                    sMODBUS_functionCode = splitCH1_Com_Cfg[5];//功能码
                    sMODBUS_registerAdd = splitCH1_Com_Cfg[6];//寄存器地址
                    sMODBUS_registerNumber = splitCH1_Com_Cfg[7];//寄存器个数
                }
                //todo   标记1

            } else if (sCH1_Com_Cfg.startsWith("ADC")) {
                String[] splitCH1_Com_Cfg = sCH1_Com_Cfg.split(",");
                sElectricity_sensor = splitCH1_Com_Cfg[1];//ADC类型
                sMaxRange = splitCH1_Com_Cfg[2];//最大量程
                sMinRange = splitCH1_Com_Cfg[3];//最小量程
                sMaxValue = splitCH1_Com_Cfg[4];//最大值
                sMinValue = splitCH1_Com_Cfg[5];//最小值
                if (sElectricity_sensor.equals("1")) {
                    sElectricity_sensor = "电流";
                } else {
                    sElectricity_sensor = "电压";
                }
                //todo   标记2
            }
        }

        //todo 标记1
        List<String> rsBaudRateList = Arrays.asList(context.getResources().getStringArray(R.array.rsBaudRate));
        dataList.add(new ParamListData("通信波特率", 1, "" + sSerial_Baudrate, rsBaudRateList, "", "", true, isChange));
        if (sSerial_Com.length() == 3) {
            dataBits = sSerial_Com.substring(0, 1);     //数据位
            stopBits = sSerial_Com.substring(2, 3);      //停止位
            checkBits = sSerial_Com.substring(1, 2);       //校验位
            switch (checkBits) {
                case "N":
                    checkBits = "无效验";
                    break;
                case "O":
                    checkBits = "奇效验";
                    break;
                case "E":
                    checkBits = "偶效验";
                    break;
            }
        }
        //数据位
        List<String> rsDataBitsList = Arrays.asList(context.getResources().getStringArray(R.array.rsDataBits));
        dataList.add(new ParamListData("数据位", 1, "" + dataBits + "位", rsDataBitsList, "", "", false, isChange));
        //停止位
        List<String> rsStopBitsList = new ArrayList<>();
        rsStopBitsList.add("1位");
        dataList.add(new ParamListData("停止位", 1, "" + stopBits + "位", rsStopBitsList, "", "", false, isChange));
        //校验位
        List<String> checkBitsList = Arrays.asList(context.getResources().getStringArray(R.array.checkBits));
        dataList.add(new ParamListData("校验位", 1, "" + checkBits, checkBitsList, "", "", false, isChange));
        List<String> rsModelList = Arrays.asList(context.getResources().getStringArray(R.array.rsModel));
//        dataList.add(new ParamListData("总线通信模式", 1, "" + getKeyFromList(sConnMode, rsModelList), rsModelList, "", "", true, false));
        sConnMode = "MODBUS";   //说好写死
        dataList.add(new ParamListData("总线通信模式", 1, "" + sConnMode, rsModelList, "", "", true, isChange));   //说好写死
        PassWayUtil.initModBus("1", dataList, sMODBUS_deviceAdd, sMODBUS_functionCode, sMODBUS_registerAdd, sMODBUS_registerNumber);

        //todo   标记2
        List<String> adcList = Arrays.asList(context.getResources().getStringArray(R.array.adcType));
        dataList.add(new ParamListData("ADC类型", 1, "" + sElectricity_sensor, adcList, "", "", false, isChange));
        dataList.add(new ParamListData("上量程", 2, "" + sMaxRange, "", "", true, isChange));
        dataList.add(new ParamListData("下量程", 2, "" + sMinRange, "", "", true, isChange));
        dataList.add(new ParamListData("最大值", 2, "" + sMaxValue, "", "", true, isChange));
        dataList.add(new ParamListData("最小值", 2, "" + sMinValue, "", "", true, isChange));


        int iIndexFi_Chn1 = sMsgObjString.indexOf(msgIndexOfFire);
        if (iIndexFi_Chn1 != -1) {
            String sFI_Chn1 = SubstringUtil.subString(sMsgObjString, msgIndexOfFire);
            if (sFI_Chn1 != null && sFI_Chn1.trim().length() > 0) {
                String[] splitHW_Chn1 = sFI_Chn1.split(",");
                if (splitHW_Chn1.length == 3) {
                    fireConfSysType = splitHW_Chn1[0];  //消防系统类型
                    fireConfPartType = splitHW_Chn1[1];  //消防部件类型
                    fireConfAnType = splitHW_Chn1[2];  //消防模拟量类型
                    //todo   标记3

                }
            }
        }

        //todo   标记3
        List<String> fireConfSysTypeList = Arrays.asList(context.getResources().getStringArray(R.array.fireConfSysType));
        dataList.add(new ParamListData("消防系统类型", 1, "" + PassWayUtil.getFireConfSysString(fireConfSysType), fireConfSysTypeList, "AT+CHFIREPARAM", "", false, isChange));

        List<String> fireConfPartTypeList = Arrays.asList(context.getResources().getStringArray(R.array.fireConfPartType));
        dataList.add(new ParamListData("消防部件类型", 1, "" + PassWayUtil.getFireConfPartString(fireConfPartType), fireConfPartTypeList, "AT+CHFIREPARAM", "", false, isChange));

        List<String> fireConfAnTypeList = Arrays.asList(context.getResources().getStringArray(R.array.fireConfAnType));
        dataList.add(new ParamListData("消防模拟量类型", 1, "" + PassWayUtil.getFireConfAnString(fireConfAnType), fireConfAnTypeList, "AT+CHFIREPARAM", "", false, isChange));


    }

    /**
     * 获取报警触发方式
     *
     * @param upperLimitValue 上限值
     * @param lowerLimitValue 下限值
     * @return
     */
    private static String getDiAlarm(String upperLimitValue, String lowerLimitValue) {
        if (upperLimitValue.equals("+0") && lowerLimitValue.equals("+0")) {
            return "关闭";
        }
        if (upperLimitValue.equals("+0") && lowerLimitValue.equals("0")) {
            return "低电平";
        }
        if (upperLimitValue.equals("1") && lowerLimitValue.equals("+0")) {
            return "高电平";
        }
        return "关闭";
    }

    /**
     * 实例化  通信参数
     *
     * @param sConnMode              总线通信模式
     * @param dataList               所有数据的list
     * @param sMODBUS_deviceAdd      设备地址
     * @param sMODBUS_functionCode   设备地址
     * @param sMODBUS_registerAdd    寄存器地址
     * @param sMODBUS_registerNumber 寄存器个数
     */
    public static void initModBus(String sConnMode, List<ParamListData> dataList, String sMODBUS_deviceAdd, String sMODBUS_functionCode, String sMODBUS_registerAdd, String sMODBUS_registerNumber) {
        switch (sConnMode) {
            case "0":
                sConnMode = "透传";
                dataList.add(new ParamListData("设备地址", 2, "", "", "", false, false));
                dataList.add(new ParamListData("功能码", 2, "", "", "", false, false));
                dataList.add(new ParamListData("寄存器地址", 2, "", "", "", false, false));
                dataList.add(new ParamListData("寄存器个数", 2, "", "", "", false, false));
                break;
            case "1":
                sConnMode = "MODBUS";
                dataList.add(new ParamListData("设备地址", 2, "" + sMODBUS_deviceAdd, "", "", true, false));
                dataList.add(new ParamListData("设备地址", 2, "" + sMODBUS_functionCode, "", "", true, false));
                dataList.add(new ParamListData("寄存器地址", 2, "" + sMODBUS_registerAdd, "", "", true, false));
                dataList.add(new ParamListData("寄存器个数", 2, "" + sMODBUS_registerNumber, "", "", true, false));
                break;
            case "2":
                sConnMode = "串口";
                dataList.add(new ParamListData("设备地址", 2, "", "", "", false, false));
                dataList.add(new ParamListData("功能码", 2, "", "", "", false, false));
                dataList.add(new ParamListData("寄存器地址", 2, "", "", "", false, false));
                dataList.add(new ParamListData("寄存器个数", 2, "", "", "", false, false));
                break;
            case "3":
                sConnMode = "MODBUS_FLOAT";
                dataList.add(new ParamListData("设备地址", 2, "" + sMODBUS_deviceAdd, "", "", true, false));
                dataList.add(new ParamListData("功能码", 2, "" + sMODBUS_functionCode, "", "", true, false));
                dataList.add(new ParamListData("寄存器地址", 2, "" + sMODBUS_registerAdd, "", "", true, false));
                dataList.add(new ParamListData("寄存器个数", 2, "" + sMODBUS_registerNumber, "", "", true, false));
                break;
            case "4":
                sConnMode = "MODBUS_BCD";
                dataList.add(new ParamListData("设备地址", 2, "" + sMODBUS_deviceAdd, "", "", true, false));
                dataList.add(new ParamListData("功能码", 2, "" + sMODBUS_functionCode, "", "", true, false));
                dataList.add(new ParamListData("寄存器地址", 2, "" + sMODBUS_registerAdd, "", "", true, false));
                dataList.add(new ParamListData("寄存器个数", 2, "" + sMODBUS_registerNumber, "", "", true, false));
                break;
            default:
                sConnMode = "无";
                break;
        }
    }

    public static String getFireConfSysString(String type) {
        String fireConfSysType = "";
        switch (type) {
            case "0":
                fireConfSysType = "无";
                break;
            case "1":
                fireConfSysType = "火灾报警系统";
                break;
            case "19":
                fireConfSysType = "防火门及卷帘系统";
                break;
            case "131":
                fireConfSysType = "消防管道压力系统";
                break;
            case "132":
                fireConfSysType = "消防水池液位系统";
                break;
            case "133":
                fireConfSysType = "市政消防栓监测系统";
                break;
            case "134":
                fireConfSysType = "消防水箱液位系统";
                break;
        }
        return fireConfSysType;
    }

    public static String getFireConfSysType(String type) {
        String fireConfSysType = "";
        switch (type) {
            case "无":
                fireConfSysType = "0";
                break;
            case "火灾报警系统":
                fireConfSysType = "1";
                break;
            case "防火门及卷帘系统":
                fireConfSysType = "19";
                break;
            case "消防管道压力系统":
                fireConfSysType = "131";
                break;
            case "消防水池液位系统":
                fireConfSysType = "132";
                break;
            case "市政消防栓监测系统":
                fireConfSysType = "133";
                break;
            case "消防水箱液位系统":
                fireConfSysType = "134";
                break;
        }
        return fireConfSysType;
    }

    public static String getFireConfPartString(String type) {
        String value = "";
        switch (type) {
            case "0":
                value = "无";
                break;
            case "1":
                value = "火灾报警控制器";
                break;
            case "23":
                value = "手动火灾报警按钮";
                break;
            case "102":
                value = "防火门";
                break;
            case "128":
                value = "消防水压";
                break;
            case "129":
                value = "消防液位";
                break;
        }
        return value;
    }

    public static String getFireConfPartType(String type) {
        String value = "";
        switch (type) {
            case "无":
                value = "0";
                break;
            case "火灾报警控制器":
                value = "1";
                break;
            case "手动火灾报警按钮":
                value = "23";
                break;
            case "防火门":
                value = "102";
                break;
            case "消防水压":
                value = "128";
                break;
            case "消防液位":
                value = "129";
                break;
        }
        return value;
    }

    public static String getFireConfAnString(String type) {
        String value = "";
        switch (type) {
            case "0":
                value = "无";
                break;
            case "4":
                value = "压力_100kPa";
                break;
            case "5":
                value = "压力_100Pa";
                break;
            case "128":
                value = "信号强度_dBm";
                break;
            case "136":
                value = "电池电压_mV";
                break;
            case "154":
                value = "液位_cm";
            case "255":
                value = "无";
                break;
        }
        return value;
    }

    public static String getFireConfAnType(String type) {
        String value = "";
        switch (type) {
            case "无":
                value = "0";
                break;
            case "压力_100kPa":
                value = "4";
                break;
            case "压力_100Pa":
                value = "5";
                break;
            case "信号强度_dBm":
                value = "128";
                break;
            case "电池电压_mV":
                value = "136";
                break;
            case "液位_cm":
                value = "154";
        }
        return value;
    }

    public static String getDeviceInface(List<ParamListData> passWayList) {
        String deviceInface = "";
        for (ParamListData data : passWayList) {
            if (data.getName().equals("硬件接口")) {
                deviceInface = data.getValue();
            }
        }
        return deviceInface;
    }

    private static String getKeyFromList(String key, List<String> stringList) {
        String value = "";
        if (!TextUtils.isEmpty(key)) {
            value = stringList.get(Integer.valueOf(key));
        }
        return value;
    }

    public static boolean getSubmitList(String sPassageWay, String hardWareInterface, List<ParamListData> passWayList, List<ParamListData> submitList) throws UnsupportedEncodingException {

        sDeviceInface = ""; //硬件接口
        sChannelOnOff = "";// 数据采样开关
        sFactorName = "";//要素标识  通道标识
        sSampBaseValue = "";//采样基值
        sUnit = "";//单位          采集数据单位
        sPrecision = "";//精度     采集数据精度
        sUnitSize = "";//采集数据大小
        sPowerOnDelay = "";//上电延时
        sStorageTimeInterval = "";//存储时间间隔
        sSampTimeInterval = "";//采样间隔
        fireConfSysType = "";  //消防系统类型
        fireConfPartType = "";  //消防部件类型
        fireConfAnType = "";  //消防模拟量类型
        sUpReportTime = "";//上报间隔
        sConnMode = "MODBUS";//总线通信模式
        sSerial_Baudrate = "";   //波特率
        sSerial_Com = "";     //数据位sSerial_Com.substring(0, 1)     停止位sSerial_Com.substring(2, 3)    校验位sSerial_Com.substring(1, 2)
        dataBits = "";     // 数据位
        stopBits = "";     // 停止位
        checkBits = "";     // 校验位
        sMODBUS_deviceAdd = "";          //设备地址
        sMODBUS_functionCode = "";       //功能码
        sMODBUS_registerAdd = "";           //寄存器地址
        sMODBUS_registerNumber = "";         //寄存器个数
        sMODBUS_universalSerialPort = "";         //通用串口
        sElectricity_sensor = "";//电流传感器       ADC类型
        sMaxRange = "";//上量程
        sMinRange = "";//下量程
        sMaxValue = "";//最大值  输入电流
        sMinValue = "";//最小值  输入电流
        upperLimitValue = "";//上限值
        lowerLimitValue = "";//下限值
        threshold = "";//报警阈值
        diAlarm = "";//报警触发方式
        sAlarmContext = "";//报警内容


        /**
         * 报警参数界面
         */
        boolean chalmIsChange = false;     //上限值、下限值、报警阈值是否被修改过
        switch (hardWareInterface) {
            case "RS485":
            case "ADC0":
            case "ADC1":
                for (ParamListData data : passWayList) {
                    switch (data.getName()) {
                        case "上限值":
                            upperLimitValue = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            chalmIsChange = getDataIsChange(chalmIsChange, data.getChange());
                            break;
                        case "下限值":
                            lowerLimitValue = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            chalmIsChange = getDataIsChange(chalmIsChange, data.getChange());
                            break;
                        case "报警阈值":
                            threshold = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            chalmIsChange = getDataIsChange(chalmIsChange, data.getChange());
                            break;
                        case "报警内容":
                            sAlarmContext = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            if (data.getChange()) {
                                submitList.add(new ParamListData("报警内容", HexUtil.chineseWordToGbk("AT+CHALMTXT=" + sPassageWay + "," + sAlarmContext) + "0D0A", "AT+CHALMTXT"));
                            }
                            break;
                    }
                }
                break;
            case "DI0":
            case "DI1":
                for (ParamListData data : passWayList) {
                    switch (data.getName()) {
                        case "报警触发方式":
                            diAlarm = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            if (data.getChange()) {
                                switch (diAlarm) {
                                    case "关闭":
                                        submitList.add(new ParamListData("报警触发方式", HexUtil.chineseWordToGbk("AT+CHALM=" + sPassageWay + ",+0,+0,0") + "0D0A", "AT+CHALM"));
                                        break;
                                    case "低电平":
                                        submitList.add(new ParamListData("报警触发方式", HexUtil.chineseWordToGbk("AT+CHALM=" + sPassageWay + ",+0,0,0") + "0D0A", "AT+CHALM"));
                                        break;
                                    case "高电平":
                                        submitList.add(new ParamListData("报警触发方式", HexUtil.chineseWordToGbk("AT+CHALM=" + sPassageWay + ",1,+0,0") + "0D0A", "AT+CHALM"));
                                        break;
                                }
                            }
                            break;
                        case "报警内容":
                            sAlarmContext = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            if (data.getChange()) {
                                submitList.add(new ParamListData("报警内容", HexUtil.chineseWordToGbk("AT+CHALMTXT=" + sPassageWay + "," + sAlarmContext) + "0D0A", "AT+CHALMTXT"));
                            }
                            break;
                    }
                }
                break;
        }

        if (chalmIsChange) {
            submitList.add(new ParamListData("上限值、下限值、报警阈值", HexUtil.chineseWordToGbk("AT+CHALM=" + sPassageWay + "," + upperLimitValue + "," + lowerLimitValue + "," + threshold) + "0D0A", "AT+CHALM"));
        }

        /**
         * 通道属性界面
         */
        boolean chparamIsChange = false;   //硬件接口,数据采样开光 , 上电延迟、通道标识、采集数据单位   是否修改
        boolean chfireparamIsChange = false;   //消防系统类型 , 消防部件类型、消防模拟量类型    是否修改
        for (ParamListData data : passWayList) {
            switch (data.getName()) {
                //通道基本参数
                case "硬件接口":
                    sDeviceInface = hardWareInterface;
                    chparamIsChange = getDataIsChange(chparamIsChange, data.getChange());
                    break;
                case "数据采样开关":
                    sChannelOnOff = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                    if (sChannelOnOff.equals("关闭")) {
                        sChannelOnOff = "0";
                    } else {
                        sChannelOnOff = "1";
                    }
                    chparamIsChange = getDataIsChange(chparamIsChange, data.getChange());
                    break;
                case "上电延迟":
                    sPowerOnDelay = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                    chparamIsChange = getDataIsChange(chparamIsChange, data.getChange());
                    break;
                case "通道标识":
                    sFactorName = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                    chparamIsChange = getDataIsChange(chparamIsChange, data.getChange());
                    break;
                case "采集数据单位":
                    sUnit = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                    chparamIsChange = getDataIsChange(chparamIsChange, data.getChange());
                    break;
                //采样基值
                case "采样基值":
                    sSampBaseValue = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                    if (!canParseInt(sSampBaseValue)) {
                        BasisApp.showToast("采样基值错误");
                        return false;
                    }
                    if (data.getChange()) {
                        submitList.add(new ParamListData("采样基值", HexUtil.chineseWordToGbk("AT+CHBASE=" + sPassageWay + "," + sSampBaseValue) + "0D0A", "AT+CHBASE"));
                    }
                    break;
                //采集数据精度
                case "采集数据精度":
                    sPrecision = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                    if (data.getChange()) {
                        submitList.add(new ParamListData("采集数据精度", HexUtil.chineseWordToGbk("AT+CHPREC=" + sPassageWay + "," + sPrecision) + "0D0A", "AT+CHPREC"));
                    }
                    break;
                //采集数据大小
                case "采集数据大小":
                    sUnitSize = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                    if (!canParseInt(sUnitSize)) {
                        BasisApp.showToast("采集数据大小错误");
                        return false;
                    }
                    if (data.getChange()) {
                        submitList.add(new ParamListData("采集数据大小", HexUtil.chineseWordToGbk("AT+CHDATASIZE=" + sPassageWay + "," + sUnitSize) + "0D0A", "AT+CHDATASIZE"));
                    }
                    break;
                //数据存储间隔
                case "数据存储间隔":
                    sStorageTimeInterval = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                    if (!canParseInt(sStorageTimeInterval)) {
                        BasisApp.showToast("数据存储间隔错误");
                        return false;
                    }
                    if (data.getChange()) {
                        submitList.add(new ParamListData("数据存储间隔", HexUtil.chineseWordToGbk("AT+CHSTOINT=" + sPassageWay + "," + sStorageTimeInterval) + "0D0A", "AT+CHSTOINT"));
                    }
                    break;
                //数据采样间隔
                case "数据采样间隔":
                    sSampTimeInterval = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                    if (!canParseInt(sSampTimeInterval)) {
                        BasisApp.showToast("数据采样间隔错误");
                        return false;
                    }
                    if (Integer.valueOf(sSampTimeInterval) > 65535 || Integer.valueOf(sSampTimeInterval) < 5) {
                        BasisApp.showToast("数据采样间隔请输入范围内的值");
                        return false;
                    }
                    if (data.getChange()) {
                        submitList.add(new ParamListData("数据采样间隔", HexUtil.chineseWordToGbk("AT+CHSAMINT=" + sPassageWay + "," + sSampTimeInterval) + "0D0A", "AT+CHSAMINT"));
                    }
                    break;
                case "消防系统类型":
                    fireConfSysType = getFireConfSysType(TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue());
                    chfireparamIsChange = getDataIsChange(chfireparamIsChange, data.getChange());
                    break;
                case "消防部件类型":
                    fireConfPartType = getFireConfPartType(TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue());
                    chfireparamIsChange = getDataIsChange(chfireparamIsChange, data.getChange());
                    break;
                case "消防模拟量类型":
                    fireConfAnType = getFireConfAnType(TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue());
                    chfireparamIsChange = getDataIsChange(chfireparamIsChange, data.getChange());
                    break;
            }

        }

        if (chfireparamIsChange) {
            submitList.add(new ParamListData("通道消防参数", HexUtil.chineseWordToGbk("AT+CHFIREPARAM=" + sPassageWay + "," + fireConfSysType + "," + fireConfPartType + "," + fireConfAnType) + "0D0A", "AT+CHFIREPARAM"));
        }
        if (chparamIsChange) {
            submitList.add(new ParamListData("通道基本参数", HexUtil.chineseWordToGbk("AT+CHPARAM=" + sPassageWay + "," + hardWareInterface + "," + sChannelOnOff + "," + sPowerOnDelay + "," + sFactorName + "," + sUnit) + "0D0A", "AT+CHPARAM"));
        }

        /**
         * 通信参数
         */

        boolean chcomIsChange = false;     //ADC类型、上量程、下量程、最大值、最小值
        boolean bitsIsChange = false;     //波特率、数据位、停止位、校验位
        boolean chComModIsChange = false;     //设备地址、功能码、寄存器地址、寄存器个数
        switch (hardWareInterface) {
            case "ADC0":
            case "ADC1":
                for (ParamListData data : passWayList) {
                    switch (data.getName()) {
                        case "ADC类型":
                            if (TextUtils.isEmpty(TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue())) {
                                BasisApp.showToast(data.getName() + "的值不能为空");
                                return false;
                            }
                            if ((TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue()).equals("电流")) {
                                sElectricity_sensor = "1";
                            } else {
                                sElectricity_sensor = "0";
                            }
                            break;
                        case "上量程":
                            if (TextUtils.isEmpty(TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue())) {
                                BasisApp.showToast(data.getName() + "的值不能为空");
                                return false;
                            }
                            sMaxRange = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            break;
                        case "下量程":
                            if (TextUtils.isEmpty(TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue())) {
                                BasisApp.showToast(data.getName() + "的值不能为空");
                                return false;
                            }
                            sMinRange = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            break;
                        case "最大值":
                            if (TextUtils.isEmpty(TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue())) {
                                BasisApp.showToast(data.getName() + "的值不能为空");
                                return false;
                            }
                            sMaxValue = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            break;
                        case "最小值":
                            if (TextUtils.isEmpty(TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue())) {
                                BasisApp.showToast(data.getName() + "的值不能为空");
                                return false;
                            }
                            sMinValue = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            break;
                    }
                    chcomIsChange = getDataIsChange(chcomIsChange, data.getChange());
                }
                break;
            case "RS484":
                for (ParamListData data : passWayList) {
                    switch (data.getName()) {
                        case "通信波特率":
                            if (TextUtils.isEmpty(TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue())) {
                                BasisApp.showToast(data.getName() + "的值不能为空");
                                return false;
                            }
                            sSerial_Baudrate = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            break;
                        case "数据位":
                            if (TextUtils.isEmpty(TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue())) {
                                BasisApp.showToast(data.getName() + "的值不能为空");
                                return false;
                            }
                            dataBits = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            break;
                        case "停止位":
                            if (TextUtils.isEmpty(TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue())) {
                                BasisApp.showToast(data.getName() + "的值不能为空");
                                return false;
                            }
                            stopBits = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            break;
                        case "校验位":
                            if (TextUtils.isEmpty(TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue())) {
                                BasisApp.showToast(data.getName() + "的值不能为空");
                                return false;
                            }
                            checkBits = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            switch (checkBits) {
                                case "无效验":
                                    checkBits = "N";
                                    break;
                                case "奇效验":
                                    checkBits = "0";
                                    break;
                                case "偶效验":
                                    checkBits = "N";
                                    break;
                            }

                            break;
                        case "设备地址":
                            if (TextUtils.isEmpty(TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue())) {
                                BasisApp.showToast(data.getName() + "的值不能为空");
                                return false;
                            }
                            sMODBUS_deviceAdd = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            if (!canParseInt(sMODBUS_deviceAdd)) {
                                BasisApp.showToast(data.getName() + "只能输入数字");
                                return false;
                            }
                            if (Integer.valueOf(sMODBUS_deviceAdd)>255 ||Integer.valueOf(sMODBUS_deviceAdd)<0) {
                                BasisApp.showToast(data.getName() + "只能输入范围内的数字");
                                return false;
                            }
                            break;
                        case "功能码":
                            if (TextUtils.isEmpty(TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue())) {
                                BasisApp.showToast(data.getName() + "的值不能为空");
                                return false;
                            }
                            sMODBUS_functionCode = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            if (!canParseInt(sMODBUS_functionCode)) {
                                BasisApp.showToast(data.getName() + "只能输入数字");
                                return false;
                            }
                            if (Integer.valueOf(sMODBUS_functionCode)>255 ||Integer.valueOf(sMODBUS_functionCode)<0) {
                                BasisApp.showToast(data.getName() + "只能输入范围内的数字");
                                return false;
                            }
                            break;
                        case "寄存器地址":
                            if (TextUtils.isEmpty(TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue())) {
                                BasisApp.showToast(data.getName() + "的值不能为空");
                                return false;
                            }
                            sMODBUS_registerAdd = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            if (!canParseInt(sMODBUS_registerAdd)) {
                                BasisApp.showToast(data.getName() + "只能输入数字");
                                return false;
                            }
                            if (Integer.valueOf(sMODBUS_registerAdd)>65535 ||Integer.valueOf(sMODBUS_registerAdd)<0) {
                                BasisApp.showToast(data.getName() + "只能输入范围内的数字");
                                return false;
                            }
                            break;
                        case "寄存器个数":
                            if (TextUtils.isEmpty(TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue())) {
                                BasisApp.showToast(data.getName() + "的值不能为空");
                                return false;
                            }
                            sMODBUS_registerNumber = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            if (!canParseInt(sMODBUS_registerNumber)) {
                                BasisApp.showToast(data.getName() + "只能输入数字");
                                return false;
                            }
                            if (Integer.valueOf(sMODBUS_registerNumber)>255 ||Integer.valueOf(sMODBUS_registerNumber)<0) {
                                BasisApp.showToast(data.getName() + "只能输入范围内的数字");
                                return false;
                            }
                            break;
                    }
                    chcomIsChange = getDataIsChange(chcomIsChange, data.getChange());
                    bitsIsChange = getDataIsChange(bitsIsChange, data.getChange());
                    chComModIsChange = getDataIsChange(chComModIsChange, data.getChange());
                }
                break;
        }
        if (chcomIsChange) {
            submitList.add(new ParamListData("通信参数", HexUtil.chineseWordToGbk("AT+CHCOM=" + sPassageWay + "," + hardWareInterface + "," + sElectricity_sensor + "," + sMaxRange + "," + sMinRange + "," + sMaxValue + "," + sMinValue) + "0D0A", "AT+CHCOM"));
        }
        if (bitsIsChange) {
            submitList.add(new ParamListData("通信参数", HexUtil.chineseWordToGbk("AT+CHCOM=" + sPassageWay + "," + hardWareInterface + "," + sSerial_Baudrate + "," + dataBits.charAt(0) + stopBits.charAt(0) + checkBits ) + "0D0A", "AT+CHCOM"));
        }
        if (chComModIsChange) {
            submitList.add(new ParamListData("通道Modbus参数", HexUtil.chineseWordToGbk("AT+CHCOMMOD=" + sPassageWay + "," + hardWareInterface + "," + "1" + ","+ sMODBUS_deviceAdd + "," + sMODBUS_functionCode + "," + sMODBUS_registerAdd + "," + sMODBUS_registerNumber) + "0D0A", "AT+CHCOMMOD"));
        }


        return true;
    }

    public static void setPassWayListChange(List<List<ParamListData>> allPassWayList) {
        for (List<ParamListData> dataList : allPassWayList) {
            for (ParamListData data : dataList) {
                data.setChange(false);
            }
        }
    }

    private static boolean getDataIsChange(boolean originalData, boolean dataIsChange) {
        if (originalData || dataIsChange) {
            return true;
        }
        return false;
    }

    public static boolean canParseInt(String str) {
        if (str == null) { //验证是否为空
            return false;
        }
        return str.matches("-?[0-9]+.*[0-9]*");
//        return str.matches("\\d+"); //使用正则表达式判断该字符串是否为数字，第一个\是转义符，\d+表示匹配1个或 //多个连续数字，"+"和"*"类似，"*"表示0个或多个
    }

    public static void hardwareInterfaceIsChange(String hardwareInterface,List<ParamListData> passWayList){
        if(!passWayList.get(0).getValue().equals(hardwareInterface)){
            passWayList.get(0).setValue(hardwareInterface);
            passWayList.get(0).setChange(true);
        }
    }

}
